<?php

/**
 * applyOnline actions.
 *
 * @package    visa
 * @subpackage applyOnline
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class applyOnlineActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
	$this->navbars = vtnsSlot::getNavBars();
	$this->submenus = vtnsSlot::getSubMenus();
	$this->rightBlocks = vtnsSlot::getRightBlocks();
	$this->menuBottoms = vtnsSlot::getMenuBottoms();
	$this->bottom = vtnsSlot::getBottom();

	$q = Doctrine_Query::create()
			->from("VTNSCmsCategory c")
			->where("alias = ?", 'apply_online');
	$cmsCategories = $q->fetchArray();
	$cms_array = array();
	foreach ($cmsCategories as $cmsCategory)
	{
		$qc = Doctrine_Query::create()
				->from("VTNSCms c")
				->where("is_active = 1")
				->addWhere("cms_category_id = ?", $cmsCategory['cms_category_id']);
		$cms_list = $qc->fetchArray();
		foreach ($cms_list as $cms)
			$cms_array[] = $cms;
	}

	$this->cms_array = $cms_array;

	if (!$request->hasParameter("func") || (($request->getParameter("func") != "applyForm") && $request->getParameter("func") != "applyForm2" )) {
		$this->redirect('default');
		return;
	}
	
	$this->step = 1;
	
	$sbo_number_applicants = $request->getParameter("sbo_number_applicants");
	$variable_0 = $request->getParameter("variable_0");
	$variable_1 = $request->getParameter("variable_1");
	$variable_2 = $request->getParameter("variable_2");
	
	$this->variable_0 = Doctrine_Core::getTable('VTNSVariable')->findOneByVariableId($variable_0);
	$this->variable_1 = Doctrine_Core::getTable('VTNSVariable')->findOneByVariableId($variable_1);
	$this->variable_2 = Doctrine_Core::getTable('VTNSVariable')->findOneByVariableId($variable_2);
	$this->sbo_number_applicants = $sbo_number_applicants;
	
	$q = Doctrine_Query::create()
			->from('VTNSRate r')
			->where('variable_id = ?', $variable_0)
			->addWhere('min_pax <= ?', $sbo_number_applicants)
			->addWhere('max_pax >= ?', $sbo_number_applicants)
			->addWhere('is_active = 1');
	$rates = $q->fetchArray();
	if (count($rates) > 0)
		$rate = $rates[0];
	else {
		$q = Doctrine_Query::create()
			->from('VTNSRate r')
			->where('variable_id = ?', $variable_0)
			->addWhere('min_pax <= ?', $sbo_number_applicants);
		$rates = $q->fetchArray();
		$rate = $rates[0];
	}
	
	$this->totalFee = 0 + ($rate['rate_value'] + $this->variable_2['variable_value']) * $sbo_number_applicants;
		
	$visa_require_cat = Doctrine_Core::getTable("VTNSCmsCategory")->findOneByAlias("visa_requirements");	
	$q = Doctrine_Query::create()
			->from("VTNSCms c")
			->where("cms_category_id = ?", $visa_require_cat->cms_category_id);
	$visa_requirements = $q->fetchArray();
	$nations = array();
	foreach ($visa_requirements as $visa_requirement) {
		$nations[$visa_requirement['cms_id']] = $visa_requirement['name'];
	}
	$this->nations = $nations;
	
	$q = Doctrine_Query::create()
			->from("VTNSVariable c")
			->where("variable_type = 3");
	$obj_port_arr = $q->fetchArray();
	$ports = array();
	foreach ($obj_port_arr as $obj_port) {
		$ports[$obj_port['variable_id']] = $obj_port['variable_name'];
	}
	$this->nations = $nations;
	$this->ports = $ports;
	
	if ($request->getParameter("func") == "applyForm2") {
		$this->step = 2;
		
		// TEMP
		$ports = array(
			0 => "Ha Noi",
			1 => "Da Nang",
			2 => "Ho Chi Minh"
		);
		
		$genders = array(
			0 => "Male",
			1 => "Female"
		);
		
		$customers = array();
		for ($i = 1; $i <= $sbo_number_applicants; $i++)
		{
			$customer = array();
			$customer["fullname"] = $request->getParameter("fullname_".$i);
			$customer["gender"] = $genders[(int)$request->getParameter("gender_".$i)];
			$customer["dayofbirth"] = $request->getParameter("dayofbirth_".$i);
			$customer["passport"] = $request->getParameter("passport_".$i);
			$customer["passport_expire"] = $request->getParameter("passport_expire_".$i);
			$customer["nationality"] = $nations[(int)$request->getParameter("nationality_".$i)];
			$customers[] = $customer;
		}
		
		$this->customers = $customers;
		
		$this->date_arrival = $request->getParameter("date_arrival");
		$this->port_arrival = $ports[(int)$request->getParameter("port_arrival")];
		$this->contact_fullname = $request->getParameter("contact_fullname");
		$this->contact_email = $request->getParameter("contact_email");
		$this->contact_country = $nations[(int)$request->getParameter("contact_country")];
	}
	
    return sfView::SUCCESS;
	
  }
}
